package com.xkdx.caipiao.my;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.jifen.JifenAction;
import com.xkdx.caipiao.presistence.jifen.JifenInfo;
import com.xkdx.caipiao.presistence.jifen.JifenModule;
import com.xkdx.caipiao.presistence.jifen.JifenPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.SharePrefenceUtil;

public class MyJifenActivity extends ThreadActivity {

	private TextView jifen;
	private ListView lv;
	private ImageView back;
	private JifenAction action;
	private JifenModule module;
	private JifenPresistence presistence;
	private String index = "1", size = "10", uid, token;
	private SharePrefenceUtil su;
	private ArrayList<JifenInfo> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_jifeninfo_caipiao);
		su = new SharePrefenceUtil(getApplicationContext(),
				SharePrefenceUtil.USERINFO);
		uid = su.getUserInfo().getId();
		token = su.getUserInfo().getLogintoken();
		findView();
		startThread();
	}

	private void findView() {
		jifen = (TextView) findViewById(R.id.my_current_jifen);
		jifen.setText("当前积分:" + su.getUserInfo().getScore());
		lv = (ListView) findViewById(R.id.lv_my_jifen);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		
		if (result.containsKey("0")) {
			list = module.list;
			if (list != null && list.size() > 0) {
				JifenAdapter adapter = new JifenAdapter();
				lv.setAdapter(adapter);
				IConstants.setListViewHeightBasedOnChildren(lv);
			}
		}
	}

	public class JifenAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.my_jifen_item_caipiao, null);

				viewHolder.type = (TextView) convertView
						.findViewById(R.id.type);

				viewHolder.time = (TextView) convertView
						.findViewById(R.id.time);
				viewHolder.jifen = (TextView) convertView
						.findViewById(R.id.jifen);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.type.setText(list.get(position).getDesc());

			viewHolder.time.setText(list.get(position).getCtime());

			viewHolder.jifen.setText(list.get(position).getCoin());

			return convertView;
		}

		class ViewHolder {

			TextView type, time, jifen;

		}

	}

	@Override
	public void startThread() {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		action = new JifenAction(uid, token, index, size);
		module = new JifenModule();
		presistence = new JifenPresistence(MyJifenActivity.this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}

	@Override
	public void onBackClick(View view) {
		finish();

	}

}
